﻿Imports Mysoft.Map.Data
Imports System.Data.SqlClient

Public Class Activity_Preview_Grid_Detail
    Inherits Mysoft.Map.Application.Security.AppPage

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim strSql As String
            strSql = GetSqlString(Request.QueryString("Type"), Request.QueryString("Location"), Session("txtDetailSql"), Request.QueryString("MainTable"))
            '为租赁的数据源做转换
            strSql = strSql.Replace("ep_customer_zl", "ep_customer")
            MainBindDataGrid(strSql)
        End If
    End Sub

    '绑定数据
    Private Sub MainBindDataGrid(ByVal strSql As String)
        Dim sadMainBind As New SqlDataAdapter(strSql, MyDB.GetSqlConnectionString)
        Dim dsMainBind As New DataSet
        Dim tmpTable As DataTable
        Try
            sadMainBind.Fill(dsMainBind)
            tmpTable = dsMainBind.Tables(0)
        Catch
            tmpTable = New DataTable
            tmpTable.Columns.Add("客户名称")
            If Request.QueryString("txtType") = "短信" Then
                tmpTable.Columns.Add("手机号码")
            Else
                tmpTable.Columns.Add("Email")
            End If
        End Try

        '自定义控件绑定数据
        Repeater1.BindMode = 1                                          '绑定模式：0 -- SQL 语句绑定，1 -- DataTable 绑定
        Repeater1.PKField = "guid"
        Repeater1.DataTable = tmpTable
        Repeater1.DataBind()                                            '绑定数据
    End Sub

    Private Function GetSqlString(ByVal sType As String, ByVal strLocation As String, ByVal strSql As String, ByVal strMainTable As String) As String
        Dim mySql As String

        If sType = "短信" Then
            mySql = strSql & " and " & strMainTable & ".MobileTel='" & strLocation & "' group by " & strMainTable & ".CstName," & strMainTable & ".mobiletel," & strMainTable & ".email"
        Else
            mySql = strSql & " and " & strMainTable & ".Email='" & strLocation & "' group by " & strMainTable & ".CstName," & strMainTable & ".mobiletel," & strMainTable & ".email"
        End If

        If mySql.IndexOf("vp_BizPreMember.CstName") > 0 Then
            mySql = mySql.Replace("CstName", "MemName")
        End If

        Return mySql
    End Function
End Class
